package com.GLM.Viewer.Circle;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.GLM.Constants.Constant;
import com.GLM.Entity.RingEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.ViewsUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;

public class MyCirclesDetailActivity extends Activity {
	private RingEntity mRingEntity;
	Button userCountBtn;
	Button circleMsgBtn;
	Button quitBtn;
	Button inviteBtn;
	private int mUserId;
	private int tempSource = -1;
	int rr_role = -1;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_mycircles_itemdetail);
		
		Intent intent = getIntent();
		mRingEntity = (RingEntity) intent.getSerializableExtra("ring");
		tempSource = intent.getIntExtra("tempSource", -1);
		mUserId = UserInfoDAO.getUserInfo(MyCirclesDetailActivity.this).getUserId();
		initViewAndData();
	}
	
	public void initViewAndData(){
		if(tempSource == 1){
			rr_role = initRrrole();
			Log.d("rr_role", rr_role+"");
		}
			
		TextView circleNameView = (TextView)findViewById(R.id.mycircle_circlename);
		TextView circleCreateUserView = (TextView)findViewById(R.id.circle_create_username);
		TextView circleCreateDate = (TextView)findViewById(R.id.circle_create_date);
		TextView circleIntroView = (TextView)findViewById(R.id.circle_introduce_detailinfo);
		userCountBtn = (Button)findViewById(R.id.circle_join_users);
		circleMsgBtn = (Button)findViewById(R.id.circle_messages);
		quitBtn = (Button)findViewById(R.id.circle_quite);
		inviteBtn = (Button)findViewById(R.id.circle_invite_friends);
		
		
		circleNameView.setText(mRingEntity.getRing_name());
		circleCreateDate.setText(getString(R.string.circle_create_time) + mRingEntity.getRing_createDate());
		circleCreateUserView.setText(getString(R.string.circle_create_name)+ mRingEntity.getCreater_nickname());
		circleIntroView.setText(mRingEntity.getRing_introduce());
		userCountBtn.setText(getString(R.string.circle_users)+ mRingEntity.getRing_user_list().length);
		circleMsgBtn.setText(getString(R.string.circle_message)+ mRingEntity.getMsg_count());
		if(tempSource == 1){
			if(rr_role == 4){
				quitBtn.setText(getString(R.string.circle_quit_join));
			}else if(rr_role == 0 || rr_role == 1){
				quitBtn.setText(getString(R.string.circle_addin));
				
			}else if(rr_role == 2 || rr_role == 3){
				quitBtn.setText(getString(R.string.mycircle_quite));
			}
			
		}
		int createId = mRingEntity.getCreater_id();
		if(createId != mUserId){
			inviteBtn.setVisibility(View.INVISIBLE);
		}
		userCountBtn.setOnClickListener(clickListeners);
		if(tempSource == 1 && rr_role == 0){
			circleMsgBtn.setClickable(false);
			
		}else{
			circleMsgBtn.setOnClickListener(clickListeners);
		}
		
		quitBtn.setOnClickListener(clickListeners);
		inviteBtn.setOnClickListener(clickListeners);
	}
	
	public OnClickListener clickListeners = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.circle_join_users:
				Intent intent = new Intent(MyCirclesDetailActivity.this, CircleDetailUsersActivty.class);
				intent.putExtra("classId", mRingEntity.getRing_id());
				startActivity(intent);
				break;
			case R.id.circle_messages:
				
				Intent msgIntent = new Intent(MyCirclesDetailActivity.this,CircleDetailMessageActivity.class);
				msgIntent.putExtra("ringId", mRingEntity.getRing_id());
				startActivity(msgIntent);
				break;
			case R.id.circle_quite:
				if(tempSource == 0){
					int status = initQuitCircle();
					if (status == 1) {
						Toast.makeText(MyCirclesDetailActivity.this, getString(R.string.circle_quit_success), Toast.LENGTH_LONG).show();
						Intent goIntent = new Intent(MyCirclesDetailActivity.this,CircleDetailUsersActivty.class);
						startActivity(goIntent);
						finish();
					}else {
						Toast.makeText(MyCirclesDetailActivity.this, getString(R.string.circle_fail), Toast.LENGTH_LONG).show();
					}
				}
				else{
					if(rr_role == 1 || rr_role == 0){
						int status = initdoActionCircle(4);
						if(status == 1){
							quitBtn.setText(getString(R.string.circle_quit_join));
						}else{
							ViewsUtil.showErrorTip(MyCirclesDetailActivity.this, R.string.global_error_networkerr);
						}
					}else if(rr_role == 4){
						initdoActionCircle(1);
					}
					
				}
				break;
			case R.id.circle_invite_friends:
				Intent inviteIntent = new Intent(MyCirclesDetailActivity.this, CircleInviteFriendsActivity.class);
				inviteIntent.putExtra("ring_id", mRingEntity.getRing_id());
				inviteIntent.putExtra("friendIds", mRingEntity.getRing_user_list());
				startActivityForResult(inviteIntent, Constant.CIRCLE_INVITE_FRIENDS);
				break;
			default:
				break;
			}
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK){
			switch (requestCode) {
			case Constant.CIRCLE_INVITE_FRIENDS:
				int status = data.getIntExtra("status", -1);
				if(status == 1){
					Toast.makeText(MyCirclesDetailActivity.this, getString(R.string.circle_invite_success), Toast.LENGTH_LONG).show();
				}
				break;

			default:
				break;
			}
		}
		
	};
	
	public int initRrrole(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		int status = -1;
		try {
			jsonData.put(Constant.PARAM_USERID, mUserId);
			jsonData.put(Constant.PARAM_RING_ID, mRingEntity.getRing_id());
			Log.d("MyCircleDetailRingId>>>", mRingEntity.getRing_id()+"");
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_RRROLE, jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				status = data.getInt(Constant.RESP_STATUS);
				Log.d("MyCircleDetailStatus>>>", status+"");
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
	
	public int initdoActionCircle(int style){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		int status = -1;
		try {
			jsonData.put(Constant.PARAM_USERID, mUserId);
			jsonData.put(Constant.PARAM_RING_ID, mRingEntity.getRing_id());
			jsonData.put("rr_role", style);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_QUITECIRCLE, jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				status = data.getInt(Constant.RESP_STATUS);
				
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
	
	
	public int initQuitCircle(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		int status = -1;
		try {
			jsonData.put(Constant.PARAM_USERID, mUserId);
			jsonData.put(Constant.PARAM_RING_ID, mRingEntity.getRing_id());
			jsonData.put("rr_role", 0);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_QUITECIRCLE, jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				status = data.getInt(Constant.RESP_STATUS);
				
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
}
